﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using Doves.Utilities;

namespace Doves.DataAccessLayer.DAO
{
    public class ContentReplacingDAO : IContentReplacing
    {
        private static DoveNewsEntities dataContext;
        private IRepository<ContentReplacing> repository;
        public ContentReplacingDAO()
        {
            dataContext = new DoveNewsEntities();
            repository = new RepositoryDAO<ContentReplacing>(dataContext);
        }

        public IEnumerable<ContentReplacing> GetAllContentReplacing()
        {
            return repository.GetAll();
        }

        public ContentReplacing GetContentReplacingById(int Id)
        {
            return repository.Find(obj => obj.Id == Id).Single();
        }

        public bool UpdateContentReplacing(ContentReplacing inContentReplacing)
        {
            if (inContentReplacing == null) return false;
            ContentReplacing upContentReplacing = repository.First(sobj => sobj.Id == inContentReplacing.Id);
            if (upContentReplacing == null) return false;
            upContentReplacing = inContentReplacing;
            return repository.SaveChanges() > 0;
        }
        public bool InsertContentReplacing(ContentReplacing inContentReplacing)
        {
            if (inContentReplacing == null) return false;
            repository.Add(inContentReplacing);
            return repository.SaveChanges() > 0;
        }


        public ReturnValue<bool> DelContentReplacing(int Id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                ContentReplacing objDel = repository.First(obj => obj.Id == Id);
                ret = DelContentReplacing(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelContentReplacing(ContentReplacing inContentReplacing)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inContentReplacing == null)
            {
                ret.Msg = "Input null";
                return ret;
            }
            try
            {
                repository.Delete(inContentReplacing);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        public IEnumerable<ContentReplacing> GetContentReplacingByArticleAnalizerId(int inArticleAnalizerId)
        {
            return dataContext.ContentReplacings.Where(a => a.ArticleAnalizerId == inArticleAnalizerId).Select(b => (ContentReplacing)b);
        }
    }
}
